package com.wind.trademark.file.dao.impl;

import java.util.Map;

import org.apache.commons.lang.StringUtils;

public class FileSql {

	public static String queryFileSQL(Map<String, Object> params) {
		
		StringBuffer stringBuffer= new StringBuffer();
		//stringBuffer.append("SELECT fi_file.*, get_file_path(fi_file.id)  path  FROM fi_file fi_file WHERE 1 = 1 ");
		stringBuffer.append("		SELECT                                          ");
		stringBuffer.append("		fi_file.*, get_file_path(fi_file.id)path,       ");
		stringBuffer.append("	IF(                                                 ");
		stringBuffer.append("		EXISTS(                                         ");
		stringBuffer.append("			SELECT                                      ");
		stringBuffer.append("				sub_file.ID                             ");
		stringBuffer.append("			FROM                                        ");
		stringBuffer.append("				fi_file sub_file                        ");
		stringBuffer.append("			WHERE                                       ");
		stringBuffer.append("				fi_file.ID = sub_file.PARENT            ");
		stringBuffer.append("		),                                              ");
		stringBuffer.append("		'true',                                         ");
		stringBuffer.append("		'false'                                         ");
		stringBuffer.append("	) HAS_CHILDREN                                      ");
		stringBuffer.append("	FROM                                                ");
		stringBuffer.append("		fi_file fi_file                                 ");
		stringBuffer.append("	WHERE                                               ");
		stringBuffer.append("		fi_file.ID<>1                                 ");
				
		
		
		if(StringUtils.isNotEmpty((String)params.get("name"))){
			stringBuffer.append(" AND fi_file.NAME LIKE :name ");
			params.put("name", "%"+params.get("name").toString()+"%");

		}
		if(StringUtils.isNotEmpty((String)params.get("parent"))){
			stringBuffer.append(" AND fi_file.PARENT = :parent ");		
		}
		if(StringUtils.isNotEmpty((String)params.get("type"))){
			stringBuffer.append(" AND fi_file.TYPE = :type ");		
		}
		if(StringUtils.isNotEmpty((String)params.get("taskId"))){
			stringBuffer.append(" AND fi_file.TASK_ID = :taskId ");		
		}
		stringBuffer.append(" ORDER BY fi_file.ID ");
		return stringBuffer.toString();
	}

	public static String queryFileVoByFileId() {
		StringBuffer stringBuffer= new StringBuffer();

		stringBuffer.append("		SELECT                                          ");
		stringBuffer.append("		fi_file.*,        ");
		stringBuffer.append("	IF(                                                 ");
		stringBuffer.append("		EXISTS(                                         ");
		stringBuffer.append("			SELECT                                      ");
		stringBuffer.append("				sub_file.ID                             ");
		stringBuffer.append("			FROM                                        ");
		stringBuffer.append("				fi_file sub_file                        ");
		stringBuffer.append("			WHERE                                       ");
		stringBuffer.append("				fi_file.ID = sub_file.PARENT            ");
		stringBuffer.append("		),                                              ");
		stringBuffer.append("		'true',                                        ");
		stringBuffer.append("		'false'                                         ");
		stringBuffer.append("	) HAS_CHILDREN                                      ");
		stringBuffer.append("	FROM                                                ");
		stringBuffer.append("		fi_file fi_file                                 ");
		stringBuffer.append(" WHERE fi_file.parent=:fileId");
 		return stringBuffer.toString();
 
	}

	public static String deleteFiles() {

		StringBuffer stringBuffer= new StringBuffer();
		stringBuffer.append("DELETE ");
		stringBuffer.append("FROM fi_file  ");
		stringBuffer.append("WHERE id IN (:ids) ");
		return stringBuffer.toString();
	
	}

}
